/**
 * @title AbsHeMaiActivity.java
 * @package com.esun.rabbit2.viewer.mainact.hemaicenteragp
 * @description 合买列表Activity的抽象父类
 * @author liugan
 * @date 2012-03-14 
 * @copyright 深圳市易讯天空网络技术有限公司
 * 
 */

package com.esun.rabbit2.viewer.mainact.hemaicenteragp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.HeMaiListInfo;
import com.esun.model.UserInfo;
import com.esun.rabbit2.communicaters.hemai.HemaiGetExpectCommunicater;
import com.esun.rabbit2.communicaters.hemai.HemaiGetListCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.ui.R;
import com.esun.util.ShowZhanji;
import com.esun.util.log.EDebug;

/**
 * @classname AbsHeMaiActivity
 * @description 合买列表Activity抽象父类
 * @author liugan
 * 
 */
public abstract class AbsHeMaiActivity extends Activity implements OnScrollListener, OnItemClickListener {

	public static final int GET_EXPECT_FAILED = 1;// 获取期号失败代码
	public static final int GET_LIST_FAILED = 2;// 获取列表失败代码

	/**
	 * @field PAGE_SIZE
	 * @description 页码记录条数
	 */
	public static final String PAGE_SIZE = "10";// 页面记录条数

	/**
	 * @descripiton 界面控件
	 */
	private RelativeLayout progressLoading; // 显示progress
	// private RadioGroup typeRadioGroup;// 排序类型RadioGroup
	// private RadioButton radioButtonHemaiJindu; //进度排列按钮
	// private RadioButton radioButtonHemaiAllMoney;//金钱数排列按钮
	// private RadioButton radioButtonHemaiZhanji;//战绩排列按钮
	// private LinearLayout linearRenQiFooter, linearZhanJiFooter,
	// linearMoneyFooter; // 列表末尾的view

	private CheckBox cbxSortRenQi; // 人气列表排序方式CheckBox
	private CheckBox cbxSortZhanJi; // 战绩列表排序方式CheckBox
	private CheckBox cbxSortMoney; // 总额列表排序方式CheckBox

	private ListView listViewRenQi;// 人气列表
	private ListView listViewZhanJi;// 战绩列表
	private ListView listViewMoney;// 总额列表
	protected ListView currentListView;// 当前显示的列表

	/**
	 * @field listRenQiAdapter
	 * @field listZhanJiAdapter
	 * @field listMoneyAdapter
	 * @description 各列表的适配器
	 */
	private HemaiListAdapter listRenQiAdapter;
	private HemaiListAdapter listZhanJiAdapter;
	private HemaiListAdapter listMoneyAdapter;

	/**
	 * @field latestRefreshTimeMills
	 * @description 保存最近一次刷新的时间
	 */
	private long latestRefreshTimeMills = 0;

	/**
	 * @title handler
	 * @description 合买Activity的handler,主要处理错误
	 */
	protected Handler handler;
	private RadioButton radioButtonHemaiJindu;
	private RadioButton radioButtonHemaiZhanji;
	private RadioButton radioButtonHemaiAllMoney;

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.hemai_activity);
		initUI();
		initHandler();
	}

	/**
	 * @title initHandler
	 * @description 初始化Handler
	 */
	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				switch (msg.what) {
				case GET_EXPECT_FAILED:
				case GET_LIST_FAILED:
					// 恢复界面状态及一些标志位
					ListView listView = (ListView) msg.obj;
					HemaiListAdapter adapter = getAdapterFromListView(listView);
					progressLoading.setVisibility(View.GONE);
					adapter.sortCheckBox.setClickable(true);
					adapter.isUpdating = false;
					// ViewerUtil.showTipDialog(AbsHeMaiActivity.this.getParent(), Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(AbsHeMaiActivity.this, Language.MSG_GET_HEMAILIST_FAILED, Toast.LENGTH_SHORT).show();

					break;
				}
				super.handleMessage(msg);
			}

		};

	}

	/**
	 * @title initUI
	 * @description 初始化界面控件
	 */
	private void initUI() {
		progressLoading = (RelativeLayout) findViewById(R.id.hemai_act_relative_progress);
		progressLoading.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		// typeRadioGroup = (RadioGroup) findViewById(R.id.hemaiact_radiogroup_sort);
		// typeRadioGroup.setOnCheckedChangeListener(new SortRadioGroupListener());
		radioButtonHemaiJindu = (RadioButton) findViewById(R.id.hemaiact_radiobutton_jindu);

		radioButtonHemaiJindu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (currentListView == listViewRenQi) {
					getListTask(listViewRenQi, false, false).execute();
				} else {
					currentListView = listViewRenQi;
					listViewRenQi.setVisibility(View.VISIBLE);
					cbxSortRenQi.setVisibility(View.VISIBLE);

					listViewZhanJi.setVisibility(View.INVISIBLE);
					cbxSortZhanJi.setVisibility(View.INVISIBLE);
					listViewMoney.setVisibility(View.INVISIBLE);
					cbxSortMoney.setVisibility(View.INVISIBLE);

					// 判断是否需要刷新列表
					if ((listRenQiAdapter.isFirstUpdating || isNeedRefresh()) && (!listRenQiAdapter.isUpdating)) {
						new GetHemaiListAsyncTask(listViewRenQi, false, false).execute();
					}
				}
			}
		});

		radioButtonHemaiZhanji = (RadioButton) findViewById(R.id.hemaiact_radiobutton_zhanji);
		radioButtonHemaiZhanji.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (currentListView == listViewZhanJi) {
					getListTask(listViewZhanJi, false, false).execute();
				} else {
					currentListView = listViewZhanJi;
					listViewRenQi.setVisibility(View.INVISIBLE);
					cbxSortRenQi.setVisibility(View.INVISIBLE);

					listViewZhanJi.setVisibility(View.VISIBLE);
					cbxSortZhanJi.setVisibility(View.VISIBLE);

					listViewMoney.setVisibility(View.INVISIBLE);
					cbxSortMoney.setVisibility(View.INVISIBLE);
					if ((listZhanJiAdapter.isFirstUpdating || isNeedRefresh()) && (!listZhanJiAdapter.isUpdating)) {
						new GetHemaiListAsyncTask(listViewZhanJi, false, false).execute();
					}
				}

			}
		});

		radioButtonHemaiAllMoney = (RadioButton) findViewById(R.id.hemaiact_radiobutton_allMoney);
		radioButtonHemaiAllMoney.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (currentListView == listViewMoney) {
					getListTask(listViewMoney, false, false).execute();
				} else {
					currentListView = listViewMoney;
					listViewRenQi.setVisibility(View.INVISIBLE);
					cbxSortRenQi.setVisibility(View.INVISIBLE);

					listViewZhanJi.setVisibility(View.INVISIBLE);
					cbxSortZhanJi.setVisibility(View.INVISIBLE);

					listViewMoney.setVisibility(View.VISIBLE);
					cbxSortMoney.setVisibility(View.VISIBLE);

					if ((listMoneyAdapter.isFirstUpdating || isNeedRefresh()) && (!listMoneyAdapter.isUpdating)) {
						new GetHemaiListAsyncTask(listViewMoney, false, false).execute();
					}
				}

			}
		});

		// linearRenQiFooter = (LinearLayout) LayoutInflater.from(this).inflate(
		// R.layout.lotteryinfoact_linear_footer, null);
		// linearRenQiFooter.setClickable(false);
		// linearZhanJiFooter = (LinearLayout) LayoutInflater.from(this).inflate(
		// R.layout.lotteryinfoact_linear_footer, null);
		// linearZhanJiFooter.setClickable(false);
		// linearMoneyFooter = (LinearLayout) LayoutInflater.from(this).inflate(
		// R.layout.lotteryinfoact_linear_footer, null);
		// linearMoneyFooter.setClickable(false);

		cbxSortRenQi = (CheckBox) findViewById(R.id.hemaiact_cbx_sortType_renqi);
		cbxSortRenQi.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!listRenQiAdapter.isUpdating) {
					getListTask(listViewRenQi, false, true).execute();
				}
			}

		});

		cbxSortZhanJi = (CheckBox) findViewById(R.id.hemaiact_cbx_sortType_zhanji);
		cbxSortZhanJi.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!listZhanJiAdapter.isUpdating) {
					getListTask(listViewZhanJi, false, true).execute();
				}
			}

		});

		cbxSortMoney = (CheckBox) findViewById(R.id.hemaiact_cbx_sortType_all_money);
		cbxSortMoney.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!listMoneyAdapter.isUpdating) {
					getListTask(listViewMoney, false, true).execute();
				}
			}

		});

		listRenQiAdapter = new HemaiListAdapter(Key.KEY_RENQI, cbxSortRenQi);
		listViewRenQi = (ListView) findViewById(R.id.hemaiact_listview_list_renqi);
		listViewRenQi.addFooterView(listRenQiAdapter.linearFooter);
		listViewRenQi.setAdapter(listRenQiAdapter);
		listViewRenQi.removeFooterView(listRenQiAdapter.linearFooter);
		listViewRenQi.setOnScrollListener(this);
		listViewRenQi.setScrollingCacheEnabled(false);
		listViewRenQi.setOnItemClickListener(this);

		// listViewRenQi.setOnRefreshListener(new OnRefreshListener() {
		//
		// @Override
		// public void onRefresh() {
		// new GetHemaiListAsyncTask(listViewRenQi, false, false, true)
		// .execute();
		//
		// }
		//
		// });

		listZhanJiAdapter = new HemaiListAdapter(Key.KEY_ZHANJI, cbxSortZhanJi);
		listViewZhanJi = (ListView) findViewById(R.id.hemaiact_listview_list_zhanji);
		listViewZhanJi.addFooterView(listZhanJiAdapter.linearFooter);
		listViewZhanJi.setAdapter(listZhanJiAdapter);
		listViewZhanJi.removeFooterView(listZhanJiAdapter.linearFooter);
		listViewZhanJi.setOnScrollListener(this);
		listViewZhanJi.setScrollingCacheEnabled(false);
		listViewZhanJi.setOnItemClickListener(this);

		listMoneyAdapter = new HemaiListAdapter(Key.KEY_ALLMONEY, cbxSortMoney);
		listViewMoney = (ListView) findViewById(R.id.hemaiact_listview_list_all_money);
		listViewMoney.addFooterView(listZhanJiAdapter.linearFooter);
		listViewMoney.setAdapter(listMoneyAdapter);
		listViewMoney.removeFooterView(listZhanJiAdapter.linearFooter);
		listViewMoney.setOnScrollListener(this);
		listViewMoney.setScrollingCacheEnabled(false);
		listViewMoney.setOnItemClickListener(this);

		currentListView = listViewRenQi;// 设置当前默认页面为人气排序列表
	}

	@Override
	protected void onResume() {

		if (getAdapterFromListView(currentListView).isFirstUpdating || isNeedRefresh()) {// 页面超时或第一次显示需要更新
			getListTask(currentListView, false, false).execute();
		}
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		ArrayList<HeMaiListInfo> currentList = getAdapterFromListView((ListView) parent).hemaiList;
		if (position < currentList.size()) {
			if (UserInfo.getUserInfo().getUserSession().length() < 3) {
				Intent intentToLogin = new Intent();
				intentToLogin.setClass(AbsHeMaiActivity.this, LoginAct.class);
				startActivity(intentToLogin);
			} else {
				String currentExpect = getAdapterFromListView((ListView) parent).expect;
				HeMaiListInfo info = currentList.get(position);
				Intent intentToHemaiDetail = new Intent();
				Bundle bundleToHemaidetail = new Bundle();
				bundleToHemaidetail.putString(Key.KEY_LOT_ID, getLotId());
				bundleToHemaidetail.putString(Key.KEY_ID, info.getID());
				bundleToHemaidetail.putString(Key.KEY_USER_NAME, info.getUserName());
				bundleToHemaidetail.putString(Key.KEY_SHOW_ID, info.getShowId());
				bundleToHemaidetail.putString(Key.KEY_ZHANJI, info.getZhanJi());
				bundleToHemaidetail.putString(Key.KEY_PLAYTYPE, info.getPlayType());
				bundleToHemaidetail.putString(Key.KEY_EXPECT, currentExpect);
				EDebug.o(this, "期号为  ==" + currentExpect);
				bundleToHemaidetail.putString(Key.KEY_ZHANJI_JINZUAN, info.getJinzuan());
				bundleToHemaidetail.putString(Key.KEY_ZHANJI_YINZUAN, info.getYinzuan());
				bundleToHemaidetail.putString(Key.KEY_ZHANJI_JINXING, info.getJinxing());
				bundleToHemaidetail.putString(Key.KEY_ZHANJI_YINXING, info.getYinxing());
				bundleToHemaidetail.putString(Key.KEY_ZHANJI_JINBEI, info.getJinbei());
				bundleToHemaidetail.putString(Key.KEY_PROID, info.getProID());
				EDebug.o(AbsHeMaiActivity.this, "the list arg2 is " + position);
				intentToHemaiDetail.putExtras(bundleToHemaidetail);
				intentToHemaiDetail.setClass(AbsHeMaiActivity.this, getHemaiDetailAcivityClass());
				startActivity(intentToHemaiDetail);
			}
		}

	}

	public abstract Class<?> getHemaiDetailAcivityClass();

	@Override
	protected void onPause() {

		super.onPause();
	}

	/**
	 * @title getAdapterFromPullListView
	 * @description 添加了footerView的ListView需要以这种方式获得Adapter
	 * @param listView
	 * @return HemaiListAdapter
	 */
	private HemaiListAdapter getAdapterFromListView(ListView listView) {
		return (HemaiListAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter();
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		ListView scrolledView = (ListView) view;
		if ((firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0)) {
			if ((!getAdapterFromListView(scrolledView).isUpdating) && (!getAdapterFromListView(scrolledView).hasNoMoreRecord)) {
				getListTask(scrolledView, true, false).execute();
			}
		}
	}

	/**
	 * @classname HemaiListAdapter
	 * @description 合买记录列表的Adapter
	 * @date 2012-03-14
	 * @author liugan
	 * 
	 */
	class HemaiListAdapter extends BaseAdapter {

		/**
		 * @field pageNo
		 * @description 保存对应列表的当前页
		 */
		int pageNo = 0;

		/**
		 * @field isFirstUpdating
		 * @description 标识对应列表是否第一次刷新
		 */
		boolean isFirstUpdating = true;

		/**
		 * @field isUpdating
		 * @description 标识对应列表是否正在刷新
		 */
		boolean isUpdating = false;

		/**
		 * @field sortField
		 * @description 记录对应的排序类型
		 */
		final String sortField;

		String expect;

		/**
		 * @field sortOrder
		 * @description 记录排序顺序
		 */
		String sortOrder = Key.KEY_DESC;

		/**
		 * @field sortCheckBox
		 * @description 持有控制对应列表排序顺序的CheckBox
		 */
		CheckBox sortCheckBox;

		/**
		 * @field hasNoMoreRecord
		 * @description 标识已经没有更多的记录
		 */
		boolean hasNoMoreRecord = false;

		/**
		 * @field linearFooter
		 * @description 刷新时显示的列表尾部
		 */
		LinearLayout linearFooter;

		/**
		 * @field linearFooterNoMore
		 * @description 无更多记录时显示列表末尾
		 */
		LinearLayout linearFooterNoMore;

		/**
		 * @field getListCommunicater
		 * @field getExpectCommunicater
		 * @description 通信用的类，每个列表都有，获取一场列表要通信两次，
		 */
		HemaiGetListCommunicater getListCommunicater = new HemaiGetListCommunicater();
		HemaiGetExpectCommunicater getExpectCommunicater = new HemaiGetExpectCommunicater();

		/**
		 * @field hemaiList
		 * @description 合买列表数据
		 */
		private ArrayList<HeMaiListInfo> hemaiList = new ArrayList<HeMaiListInfo>();

		/**
		 * @title HemaiListAdapter
		 * @description 构造方法
		 * @param sortField
		 * @param sortCheckBox
		 */
		public HemaiListAdapter(String sortField, CheckBox sortCheckBox) {
			super();
			this.sortField = sortField;
			this.sortCheckBox = sortCheckBox;
			this.linearFooter = (LinearLayout) LayoutInflater.from(AbsHeMaiActivity.this).inflate(R.layout.lotteryinfoact_linear_footer, null);
			this.linearFooterNoMore = (LinearLayout) LayoutInflater.from(AbsHeMaiActivity.this).inflate(R.layout.lotteryinfo_list_footer_nomore, null);
		}

		public ArrayList<HeMaiListInfo> getAdapterList() {
			return hemaiList;
		}

		@Override
		public int getCount() {
			return hemaiList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewTag viewTag;
			if (convertView == null) { // convertView不为空则代表其可充当子View重复利用
				convertView = LayoutInflater.from(AbsHeMaiActivity.this).inflate(R.layout.hemailist_linear_item, null);
				viewTag = new ViewTag();
				viewTag.tvListItemJindu = (TextView) convertView.findViewById(R.id.hemailistlinearitem_tv_jindu);
				viewTag.tvListItemFaqiren = (TextView) convertView.findViewById(R.id.hemailistlinearitem_tv_faqiren);
				viewTag.tvListItemAllMoney = (TextView) convertView.findViewById(R.id.hemailistlinearitem_tv_allMoney);
				viewTag.tvListItemEachMoney = (TextView) convertView.findViewById(R.id.hemailistlinearitem_tv_eachMoney);
				viewTag.llZhanJi = (LinearLayout) convertView.findViewById(R.id.hemailistlinearitem_linear_zhanji);
				viewTag.llJinDuBackground = (LinearLayout) convertView.findViewById(R.id.hemailist_top_linear_jindu_background);
				viewTag.linearListItemMain = (LinearLayout) convertView.findViewById(R.id.hemailistlinearitem_linear_main);
				convertView.setTag(viewTag);
			} else {
				viewTag = (ViewTag) convertView.getTag();
			}

			if (hemaiList.get(position).getOrderOrNot().equals(Config.NUM0)) {
				viewTag.linearListItemMain.setBackgroundResource(R.drawable.relative_gcjl_bg_style);
			} else {
				viewTag.linearListItemMain.setBackgroundResource(R.drawable.relative_hemailancher_listitem_style);
			}

			// 通过ViewTag使用被重复利用的子View上的控件
			viewTag.llJinDuBackground.setBackgroundResource(getResourcesForJindu(hemaiList.get(position).getJinDu()));
			viewTag.tvListItemJindu.setText(hemaiList.get(position).getJinDu() + Config.PERCENT);
			viewTag.tvListItemFaqiren.setText(getStringFaqiren(hemaiList.get(position).getUserName()));
			viewTag.tvListItemAllMoney.setText(Language.POPLOTTERYTIP_RMB + hemaiList.get(position).getAllMoney());
			viewTag.tvListItemEachMoney.setText(Language.POPLOTTERYTIP_RMB + hemaiList.get(position).getOneMoney());

			// 处理战绩
			String golddiamond = hemaiList.get(position).getJinzuan();
			// 银钻
			String silverdiamond = hemaiList.get(position).getYinzuan();
			// 金星
			String goldstar = hemaiList.get(position).getJinxing();
			// 银星
			String silverstar = hemaiList.get(position).getYinxing();
			// 金杯
			String goldcup = hemaiList.get(position).getJinbei();
			ShowZhanji zhanji = new ShowZhanji(AbsHeMaiActivity.this);
			// 获取显示战绩星级图片的list
			List<ImageView> zhanjilist = zhanji.showzhanji(golddiamond, silverdiamond, goldstar, silverstar, goldcup);
			// 判断是否有战绩
			int size = zhanjilist.size();
			if (size == 0) {// 没有战绩就不显示战绩的linear
				viewTag.llZhanJi.setVisibility(View.GONE);
			} else {// 有战绩就将list中的ImageView添加到显示战绩的linear
				for (int i = 0; i < zhanjilist.size(); i++) {
					viewTag.llZhanJi.addView(zhanjilist.get(i));
				}
			}

			// int iZhanji = Integer.parseInt(hemaiList.get(position).getZhanJi());
			// if (iZhanji / 100000 != 0) {
			// viewTag.ivGoldDiamond.setVisibility(View.VISIBLE);
			// if (iZhanji / 100000 < 10) {
			// viewTag.ivGoldDiamond.setBackgroundResource(goldDiamondResources[(iZhanji / 100000) - 1]);
			// } else {
			// viewTag.ivGoldDiamond.setBackgroundResource(goldDiamondResources[9]);
			// }
			// iZhanji = iZhanji % 100000;
			// }
			// if (iZhanji / 10000 != 0) {
			// viewTag.ivGoldStar.setVisibility(View.VISIBLE);
			// viewTag.ivGoldStar.setBackgroundResource(goldStarResources[(iZhanji / 10000) - 1]);
			// iZhanji = iZhanji % 10000;
			// }
			// if (iZhanji / 1000 != 0) {
			// viewTag.ivSilverDiamond.setVisibility(View.VISIBLE);
			// viewTag.ivSilverDiamond.setBackgroundResource(silverDiamondResources[(iZhanji / 1000) - 1]);
			// iZhanji = iZhanji % 1000;
			// }
			// if (iZhanji / 100 != 0) {
			// viewTag.ivSilverStar.setVisibility(View.VISIBLE);
			// viewTag.ivSilverStar.setBackgroundResource(silverStarResources[(iZhanji / 100) - 1]);
			// iZhanji = iZhanji % 100;
			// }

			return convertView;
		}

		/**
		 * @title getResourcesForJindu
		 * @description 获得进度的对应图标
		 * @param strJindu
		 * @return 图标ID
		 */
		private int getResourcesForJindu(String strJindu) {
			int i = Integer.parseInt(strJindu);
			i = (i - 1) / 10;
			// EDebug.o(this, "进度代码= " + i);

			if (i < 9) {
				return jinDuResources[i];
			} else {
				return jinDuResources[8];
			}
		}

		/**
		 * @title getStringFaqiren
		 * @description 获得发起人的名字
		 * @param strName
		 * @return
		 */
		private String getStringFaqiren(String strName) {
			String strShort = strName;
			if (strShort.length() < 6) {
				return strShort;
			} else {
				strShort = strShort.substring(0, 4) + Config.ELLIPSIS;
				return strShort;
			}
		}

		/**
		 * @classname ViewTag
		 * @description 用来记录循环利用的Item的View中的控件，节省通过ID来找的时间
		 * @author liugan
		 * 
		 */
		class ViewTag {
			TextView tvListItemJindu;
			TextView tvListItemFaqiren;
			TextView tvListItemAllMoney;
			TextView tvListItemEachMoney;
			LinearLayout llZhanJi;
			LinearLayout llJinDuBackground;
			LinearLayout linearListItemMain;
		}

	}

	/**
	 * @title isNeedRefresh
	 * @description 是否超时刷新
	 * @return 是否需要刷新
	 */
	public boolean isNeedRefresh() {
		if (System.currentTimeMillis() - latestRefreshTimeMills > Config.HEMAILIST_REFRESH_TIME) {
			return true;
		}
		return false;
	}

	/**
	 * @title resetLatestRefreshTimeMills
	 * @description 重置最后刷新时间
	 */
	private void resetLatestRefreshTimeMills() {
		this.latestRefreshTimeMills = System.currentTimeMillis();
	}

	/**
	 * @classname GetHemaiListAsyncTask
	 * @description 获得合买列表的异步工作类
	 * @author liugan
	 * 
	 */
	class GetHemaiListAsyncTask extends AsyncTask<Void, Void, ArrayList<HeMaiListInfo>> {

		/**
		 * @field listView
		 * @description 持有listView的引用
		 */
		ListView listView;
		/**
		 * @field adapter
		 * @description 持有listView对应的Adapter
		 */
		HemaiListAdapter adapter;

		/**
		 * @field expectInTask
		 * @description 获得的期号
		 */
		String expectInTask;

		/**
		 * @field
		 * @description 标识是否是获得更多列表项的请求
		 */
		boolean addToBottom;

		/**
		 * @field sortRefresh
		 * @description 标识是否是排序顺序方式改变引起的刷新
		 */
		boolean sortRefresh;

		boolean bSucceed = false;

		// /**
		// * @field
		// * @description 标识是否是拉取刷新
		// */
		// boolean pullToRefresh;

		/**
		 * @title GetHemaiListAsyncTask
		 * @description 构造方法
		 * @param pullToRefreshListView
		 * @param addToBottom
		 * @param sortRefresh
		 * @param pullToRefresh
		 */
		public GetHemaiListAsyncTask(ListView listView, boolean addToBottom, boolean sortRefresh) {
			this.listView = listView;
			this.adapter = getAdapterFromListView(listView);
			this.addToBottom = addToBottom;
			this.sortRefresh = sortRefresh;
			// this.pullToRefresh = pullToRefresh;
		}

		@Override
		protected void onPreExecute() {// UI线程中执行，发起通信之前的一些操作

			adapter.sortCheckBox.setClickable(false);
			if (addToBottom) {
				listView.addFooterView(adapter.linearFooter);
			} else {
				progressLoading.setVisibility(View.VISIBLE);
			}
			adapter.isUpdating = true;
			super.onPreExecute();
		}

		@Override
		protected ArrayList<HeMaiListInfo> doInBackground(Void... params) { // 工作线程中执行
			boolean bGetExpectSucceed = false;
			try {
				String expect = getLotId();
				if (expect.equals(Config.RENXUANJIU)) {
					expect = Config.SHENGFUCAI;
				}
				if (bGetExpectSucceed = adapter.getExpectCommunicater.isGetExpectNumberSucceed(expect)) {
					expectInTask = adapter.getExpectCommunicater.getExpect();
				}
			} catch (Exception e) {
				Message msg = handler.obtainMessage(GET_EXPECT_FAILED, this.listView);
				handler.sendMessage(msg);
				e.printStackTrace();
			}

			ArrayList<HeMaiListInfo> resultList = new ArrayList<HeMaiListInfo>();
			if (bGetExpectSucceed) {
				// 先不保存列表顺序到adapter，通信成功后再存储，因为通信失败后不方便改回来
				String sortOrder = adapter.sortOrder;
				if (sortRefresh) {
					if (Key.KEY_ASC.equals(sortOrder)) {
						sortOrder = Key.KEY_DESC;
					} else {
						sortOrder = Key.KEY_ASC;
					}
				}

				String pageNumber = "1";
				if (addToBottom) {
					pageNumber = adapter.pageNo + 1 + "";
				}

				try {
					String expect = getLotId();
					if (expect.equals(Config.RENXUANJIU)) {
						expect = Config.SHENGFUCAI;
					}
					if (bSucceed = adapter.getListCommunicater.isGetHemaiListSucceed(getMid(), expect, getPlayId(), expectInTask, adapter.sortField, sortOrder, PAGE_SIZE, pageNumber + "")) {

						resultList = adapter.getListCommunicater.getHeMaiList();
					} else {
						Message msg = handler.obtainMessage(GET_LIST_FAILED, this.listView);
						handler.sendMessage(msg);
					}

				} catch (Exception e) {
					Message msg = handler.obtainMessage(GET_LIST_FAILED, this.listView);
					handler.sendMessage(msg);
					e.printStackTrace();
				}

			} else {
				Message msg = handler.obtainMessage(GET_EXPECT_FAILED, this.listView);
				handler.sendMessage(msg);
			}

			return resultList;
		}

		@Override
		protected void onPostExecute(ArrayList<HeMaiListInfo> result) {
			if (bSucceed) {
				adapter.expect = expectInTask;
				adapter.sortCheckBox.setClickable(true);

				// 排列顺序改变刷新成功时改变其标识
				if (sortRefresh) {
					if (Key.KEY_ASC.equals(adapter.sortOrder)) {
						adapter.sortOrder = Key.KEY_DESC;
					} else {
						adapter.sortOrder = Key.KEY_ASC;
					}

				}

				// 第一次刷新时设置
				if (adapter.isFirstUpdating) {
					adapter.isFirstUpdating = false;
				}

				if (addToBottom) { // 获取更多时
					listView.removeFooterView(adapter.linearFooter);
					adapter.getAdapterList().addAll(result);

				} else { // 刷新时
					progressLoading.setVisibility(View.INVISIBLE);
					adapter.pageNo = 0;
					adapter.getAdapterList().clear();
					adapter.getAdapterList().addAll(result);
					if (adapter.hasNoMoreRecord) { // 之前的状态是没有更多记录
						listView.removeFooterView(adapter.linearFooterNoMore);
						adapter.hasNoMoreRecord = false;
					}
				}

				if (result.size() < Integer.parseInt(PAGE_SIZE)) {
					listView.addFooterView(adapter.linearFooterNoMore);
					adapter.hasNoMoreRecord = true;
				}

				adapter.notifyDataSetChanged();
				resetLatestRefreshTimeMills();
				adapter.pageNo++;
				adapter.isUpdating = false;
				result.clear();
			}
			super.onPostExecute(result);
		}

	}

	/**
	 * @title refresh
	 * @description 外部调用刷新
	 */
	public void refresh() {
		if (!getAdapterFromListView(currentListView).isUpdating) {
			getListTask(currentListView, false, false).execute();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Activity parent = getParent();
			if (parent != null) {
				parent.onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * @title getMid
	 * @description 获得彩种的Mid
	 * @return Mid
	 */
	public abstract String getMid();

	/**
	 * @title getLotId
	 * @description 获得彩种的LotId
	 * @return LotId
	 */
	public abstract String getLotId();

	/**
	 * @title getPlayId
	 * @description 获得彩种的PlayId
	 * @return PlayId
	 */
	public abstract String getPlayId();

	public static int[] silverStarResources = { R.drawable.cyhm_icon_silverstar1_default_21x18, R.drawable.cyhm_icon_silverstar2_default_21x18, R.drawable.cyhm_icon_silverstar3_default_21x18,
			R.drawable.cyhm_icon_silverstar4_default_21x18, R.drawable.cyhm_icon_silverstar5_default_21x18, R.drawable.cyhm_icon_silverstar6_default_21x18,
			R.drawable.cyhm_icon_silverstar7_default_21x18, R.drawable.cyhm_icon_silverstar8_default_21x18, R.drawable.cyhm_icon_silverstar9_default_21x18,
			R.drawable.cyhm_icon_silverstar9m_default_27x18 };
	public static int[] silverDiamondResources = { R.drawable.cyhm_icon_silverdiamond1_default_21x15, R.drawable.cyhm_icon_silverdiamond2_default_21x15,
			R.drawable.cyhm_icon_silverdiamond3_default_21x15, R.drawable.cyhm_icon_silverdiamond4_default_21x15, R.drawable.cyhm_icon_silverdiamond5_default_21x15,
			R.drawable.cyhm_icon_silverdiamond6_default_21x15, R.drawable.cyhm_icon_silverdiamond7_default_21x15, R.drawable.cyhm_icon_silverdiamond8_default_21x15,
			R.drawable.cyhm_icon_silverdiamond9_default_21x15, R.drawable.cyhm_icon_silverdiamond9m_default_21x15 };
	public static int[] goldStarResources = { R.drawable.cyhm_icon_goldenstar1_default_21x15, R.drawable.cyhm_icon_goldenstar2_default_21x15, R.drawable.cyhm_icon_goldenstar3_default_21x15,
			R.drawable.cyhm_icon_goldenstar4_default_21x15, R.drawable.cyhm_icon_goldenstar5_default_21x15, R.drawable.cyhm_icon_goldenstar6_default_21x15,
			R.drawable.cyhm_icon_goldenstar7_default_21x15, R.drawable.cyhm_icon_goldenstar8_default_21x15, R.drawable.cyhm_icon_goldenstar9_default_21x15,
			R.drawable.cyhm_icon_goldenstar9m_default_27x15 };
	public static int[] goldDiamondResources = { R.drawable.cyhm_icon_goldendiamond1_default_21x15, R.drawable.cyhm_icon_goldendiamond2_default_21x15,
			R.drawable.cyhm_icon_goldendiamond3_default_21x15, R.drawable.cyhm_icon_goldendiamond4_default_21x15, R.drawable.cyhm_icon_goldendiamond5_default_21x15,
			R.drawable.cyhm_icon_goldendiamond6_default_21x15, R.drawable.cyhm_icon_goldendiamond7_default_21x15, R.drawable.cyhm_icon_goldendiamond8_default_21x15,
			R.drawable.cyhm_icon_goldendiamond9_default_21x15, R.drawable.cyhm_icon_goldendiamond9m_default_27x15 };

	public static int[] jinDuResources = { R.drawable.cyhm_icon_jdq10_default_54x54, R.drawable.cyhm_icon_jdq20_default_54x54, R.drawable.cyhm_icon_jdq30_default_54x54,
			R.drawable.cyhm_icon_jdq40_default_54x54, R.drawable.cyhm_icon_jdq50_default_54x54, R.drawable.cyhm_icon_jdq60_default_54x54, R.drawable.cyhm_icon_jdq70_default_54x54,
			R.drawable.cyhm_icon_jdq80_default_54x54, R.drawable.cyhm_icon_jdq90_default_54x54 };

	class GetHemaiListAsyncTaskWithoutExpect extends GetHemaiListAsyncTask {

		/**
		 * @param listView
		 * @param addToBottom
		 * @param sortRefresh
		 */
		public GetHemaiListAsyncTaskWithoutExpect(ListView listView, boolean addToBottom, boolean sortRefresh) {
			super(listView, addToBottom, sortRefresh);
		}

		@Override
		protected ArrayList<HeMaiListInfo> doInBackground(Void... params) {
			ArrayList<HeMaiListInfo> resultList = new ArrayList<HeMaiListInfo>();

			// 先不保存列表顺序到adapter，通信成功后再存储，因为通信失败后不方便改回来
			String sortOrder = adapter.sortOrder;
			if (sortRefresh) {
				if (Key.KEY_ASC.equals(sortOrder)) {
					sortOrder = Key.KEY_DESC;
				} else {
					sortOrder = Key.KEY_ASC;
				}
			}

			String pageNumber = "1";
			if (addToBottom) {
				pageNumber = adapter.pageNo + 1 + "";
			}

			try {
				if (bSucceed = adapter.getListCommunicater.isGetHemaiListSucceed(getMid(), getLotId(), getPlayId(), null, adapter.sortField, sortOrder, PAGE_SIZE, pageNumber + "")) {

					resultList = adapter.getListCommunicater.getHeMaiList();
				} else {
					Message msg = handler.obtainMessage(GET_LIST_FAILED, this.listView);
					handler.sendMessage(msg);
				}

			} catch (Exception e) {
				Message msg = handler.obtainMessage(GET_LIST_FAILED, this.listView);
				handler.sendMessage(msg);
				e.printStackTrace();
			}

			return resultList;
		}

	}

	protected abstract GetHemaiListAsyncTask getListTask(ListView listView, boolean addToBottom, boolean sortRefresh);

}
